    package cn.zl.biz.service.impl;

    import cn.zl.biz.entity.SysArticle;
    import cn.zl.common.utils.QueryPage;
    import cn.zl.biz.entity.SysCategory;
    import cn.zl.biz.mapper.CategoryMapper;
    import cn.zl.biz.service.ArticleCategoryService;
    import cn.zl.biz.service.CategoryService;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import javax.annotation.Resource;
    import java.util.List;

    /**
     * @author zl
     * @date 2023/9/12
     */
    @Service
    public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, SysCategory> implements CategoryService {

        @Resource
        private CategoryMapper categoryMapper;

        @Resource
        private ArticleCategoryService articleCategoryService;

        //查询分类列表页
        @Override
        public List<SysCategory> findAllCategorys() {
            return categoryMapper.findAllCategorys();
        }

        //根究分类id查询文章列表
        @Override
        public List<SysArticle> findAllArticle(Integer categoryId) {
            return articleCategoryService.getAllArticleByCategoryId(categoryId);
        }

        //根据给定的文章分类对象和查询条件，分页获取文章分类列表
        @Override
        public IPage<SysCategory> list(SysCategory sysCategory, QueryPage queryPage) {
            IPage<SysCategory> page = new Page<>(queryPage.getPage(), queryPage.getLimit());
            LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(sysCategory.getName()), SysCategory::getName, sysCategory.getName());
            queryWrapper.orderByDesc(SysCategory::getId);
            return categoryMapper.selectPage(page, queryWrapper);
        }

        //根据给定的文章分类对象获取文章分类列表
        @Override
        public List<SysCategory> list(SysCategory sysCategory) {
            LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(sysCategory.getName()), SysCategory::getName, sysCategory.getName());
            queryWrapper.orderByDesc(SysCategory::getId);
            return categoryMapper.selectList(queryWrapper);
        }

        //新增文章分类
        @Override
        @Transactional
        public void add(SysCategory sysCategory) {
            if (!exists(sysCategory)) {
                categoryMapper.insert(sysCategory);
            }
        }

        //更新文章分类信息
        @Override
        @Transactional
        public void update(SysCategory sysCategory) {
            categoryMapper.updateById(sysCategory);
        }

        //检查是否已存在相同名称的文章分类
        private boolean exists(SysCategory sysCategory) {
            LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysCategory::getName, sysCategory.getName());
            return categoryMapper.selectList(queryWrapper).size() > 0 ? true : false;
        }

        //删除指定的文章分类
        @Override
        @Transactional
        public void delete(Long id) {
            categoryMapper.deleteById(id);
            //删除与该分类与文章关联的信息
            articleCategoryService.deleteByCategoryId(id);
        }

        //根据文章ID获取与之相关的文章分类列表
        @Override
        public List<SysCategory> findByArticleId(Long id) {
            return categoryMapper.findCategoryByArticleId(id);
        }
    }
